<?php

/**
 * 
 * @author yuxun
 *
 */
class IosExamineService extends Service{
	
	public static function indexHeader(){
		return array(
				'id'         => array('text' => 'ID', 'rule' => 'int'),
				'name'       => array('text' => '名称'),
				'packet'    => array('text' => '包号'),
				'v_num'      => array('text' => '版本号'),
				'type'       => array('text' => '类型'),
				'catname'    => array('text' => '地址分类'),
				'game_notice_url'       => array('text' => '游戏公告地址'),
				'game_sever_list_url'       => array('text' => '游戏服务器地址'),
				'login_server_address'       => array('text' => '登录服务器地址'),
				'login_server_port'       => array('text' => '登录服务器端口'),
				'resource_update_address'       => array('text' => '资源更新地址'),
				'channel_platform_id' => array('text' => '资源渠道ID'),
				'channel' => array('text' => '渠道'),
				'create_time'     => array('text' => '添加时间'),
				'action'     => array('text' => '操作')
		);
	}
	
	public static function indexData($params){
		$modIosExamine = new ModIosExamine();
		$result = $modIosExamine->page($params);
		$length = count($result['data']);
		$typeList = self::typeList();
		foreach($result['data'] as &$v){
			$type = 1;
			if($k == 0 && $length == 1){  //当前页只剩下一条数据时，删除之后需显示上一页的数据
				$type = 2;
			}
			
			$ios_type = "";
			if($v['type'] == 2) {
				$ios_type = "<span class='changeVisible yes' data-id='".$v['id']."'><i class='fa fa-check-circle'></i>正式服</span>";
			}
			if($v['type'] == 1){
				$ios_type = "<span class='changeVisible no' data-id='".$v['id']."'><i class='fa fa-ban'></i>审核服</span>";
			}
				
			$v['type'] = $ios_type;
			$v['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
			$v['action']  .= ' <a href="'.PubFunc::buildBackUrl('/oss/?r=iosExamine/edit').'&id='.$v['id'].'">编辑</a>';
			$v['action']  .= ' <a href="'.PubFunc::buildBackUrl('/oss/?r=iosExamine/delete',$type).'&id='.$v['id'].'" onclick="del()" confirm="是否要删除该条服信息?">删除</a>';
		}
		unset($v);
		return $result;
	}
	
	public static function doAdd($params){
		$user = AccAuth::me();
	
		$modIosExamine = new ModIosExamine();
		$modIosExamine->game = $params['g'];
		$modIosExamine->packet   = $params['packet'];
		$modIosExamine->name  = $params['name'];
		$modIosExamine->v_num  = $params['v_num'];	
		$modIosExamine->create_user   = $user['UserID'];
		$modIosExamine->create_time   = time();
		$modIosExamine->update_user   = $user['UserID'];
		$modIosExamine->update_time   = time();
		$modIosExamine->type   = $params['type'];
		$modIosExamine->category   = $params['category'];
		$modIosExamine->channel = $params['channel'];
		$result = $modIosExamine->save();
		return array('state' => true, 'msg' => '成功增加');
	}
	
	public static function get($id){
		$modIosExamine = new ModIosExamine();
		$obj = $modIosExamine->findByPk($id);
	
		if($obj){
			$info = $obj->getAttributes();
			return $info;
		}else{
			return false;
		}
	}
	
	public static function doEdit($id, $params){
		if(empty($id)) return array('state' => false, 'msg' => '信息不存在');

		$user = AccAuth::me();
		$modIosExamine = new ModIosExamine();
		$update = array();
	
		$update['packet'] = $params['packet'];
		$update['v_num'] = $params['v_num'];
		$update['name'] = $params['name'];
		$update['update_user']  = $user['UserID'];
		$update['update_time']  = time();
		
		$update['category'] = $params['category'];
		$update['channel'] = $params['channel'];
		
		$result = $modIosExamine->updateByPk($id, $update);

		return true;
	}
	
	public static function delete($id){
		if(empty($id)) return false;		
		$modIosExamine = new ModIosExamine();
		$modIosExamine->deleteByPk($id);
		return array('state' => true, 'msg' => '成功删除');
	}
	
	
	/*
	 * 去重
	 */
	public static function repeat($id = 0, $field1, $val1,$field2, $val2,$g){
		$modIosExamine = new ModIosExamine();
		$conut = $modIosExamine->repeat($id, $field1, $val1,$field2, $val2,$g);
		return $conut;
	}
	
	public static function typeList(){
		$typeList = array(
				'1'=>'审核服',
				'2'=>'正式服',
		);
		return $typeList;
	}
	
	
	public static function change_field($id,$field,$value){
		$attr = array(
				$field      => $value,
		);
		try{
			$modIosExamine = new ModIosExamine();
			$modIosExamine->updateByPk($id, $attr);
			return true;
		}catch (Exception $e){
			return false;
		}
	
	}
	
	public static function getForCategory($category){
		$game = PubBase::getCurrentGame();
		$params = array();
		$params['g'] = $game;
		$params['category'] = $category;
		$modIosExamine = new ModIosExamine();
		$data = $modIosExamine->select($params);
		if(count($data) > 0){
			return true;
		}else{
			return false;
		}
	}


}